package com.yyfax.ph.products;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.yyfax.common.adapter.BaseRecycleAdapter;
import com.yyfax.common.recyclerview.IViewHolder;
import com.yyfax.common.recyclerview.OnItemClickListener;
import com.yyfax.ph.R;
import com.yyfax.ph.base.YJPH;

/**
 * Created by fan on 2017/11/6.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class ProductsAdapter extends BaseRecycleAdapter<ProductsModel.Product> {

	private OnItemClickListener<ProductsModel.Product> mClickListener;

	public ProductsAdapter(OnItemClickListener listener) {
		mClickListener = listener;
	}


	@Override
	public IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_item, null);
		return new ViewHolder(view);
	}

	@Override
	public void onBindViewHolder(IViewHolder holder, final int position) {
		final ProductsModel.Product product = getDataSet().get(position);
		final ViewHolder viewHolder = (ViewHolder) holder;
		viewHolder.mNameTv.setText(product.name);
		viewHolder.mDescriptionTv.setText(product.description);
		YJPH.load(((ViewHolder) holder).itemView.getContext(), product.imageUrl, viewHolder.mIv);
		viewHolder.mProductItemLy.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				mClickListener.onItemClick(position, product, viewHolder.mProductItemLy);
			}
		});
	}


	@Override
	public int getItemCount() {
		return getCount();
	}

	static class ViewHolder extends IViewHolder {
		@BindView(R.id.iv)
		ImageView mIv;
		@BindView(R.id.name_tv)
		TextView mNameTv;
		@BindView(R.id.description_tv)
		TextView mDescriptionTv;
		@BindView(R.id.product_item_ly)
		LinearLayout mProductItemLy;

		ViewHolder(View view) {
			super(view);
			ButterKnife.bind(this, view);
		}
	}
}
